//
// Created by andrew on 2022/5/12.
//

#include <iostream>
#include <string>
#include <unordered_map>


using namespace std;


class Solution {
private:
    unordered_map<char, int> symbolsValues = {
            {'I', 1},
            {'V', 5},
            {'X', 10},
            {'L', 50},
            {'C', 100},
            {'D', 500},
            {'M', 1000}
    };

public:
    int romanToInt(string s) {

        int length = s.length();
        int ans = 0;
        for (int i = 0; i < length; i++) {
            if (i < length - 1 && symbolsValues[s[i]] < symbolsValues[s[i + 1]]) {
                ans -= symbolsValues[s[i]];
            } else {
                ans += symbolsValues[s[i]];
            }
        }

        return ans;
    }
};




#include <iostream>
#include <gtest/gtest.h>

TEST(Roman, intToRoman) {

    Solution solution;
    EXPECT_EQ(1500, solution.romanToInt("MD"));
}


